<template>
    <el-dialog
            :visible.sync="dialogVisible"
            width="600px"
            :before-close="closeDialog"
            append-to-body
            title="关联角色"
    >
    
        <div class="drawer-panel">
            <div class="search-item">
                <el-input
                        size="small"
                        v-model="queryParams.roleCode"
                        class="input-width clear-input"
                        suffix-icon="el-icon-search"
                        clearable
                        placeholder="请输入查询内容"
                ></el-input>
            </div>
            <div class="search-item">
                <el-button type="primary" size="small"  @click="queryRoleList">搜索</el-button>
            </div>
        
            <div>
                <el-table
                    :data="tableData"
                    size="small"
                    border
                    :row-style="{height:'0'}"
                    style="width: 100%"
                    height="400"
                    @current-change="handleDataChange"
                    highlight-current-row
                    v-loading="loading">
                    <el-table-column
                            v-for="(item,index) in fields"
                            :key="index"
                            :label="item.label"
                            :prop="item.prop"
                            :min-width="item.width"
                            :fixed="item.fixed"
                    >
                    </el-table-column>
                </el-table>
                <div class="page-container" style="float:left;margin-top:10px">
                    <el-pagination
                            :page-sizes="[10, 20, 30, 50]"
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page.sync="nowPage"
                            :page-size="10"
                            layout="sizes,total, prev, pager, next"
                            :total="total">
                    </el-pagination>
                </div>
            </div>

            <div class="btn-group" style="margin-top:40px">
                <el-button type="text" size="small" plain @click="closeDialog">取消</el-button>
                <el-button type="primary"  size="small" :disabled="loading" @click="submit">确定</el-button>
            </div>
        </div>
        
    </el-dialog>
</template>

<script>

    import {getRoleList} from '@/api/role';
    export default {
        data(){
            return {
                labelPosition:'center',
                dialogVisible: false,
                total: 0,
                loading: false,
                nowPage:1,
                queryParams:{
                    pageNum:1,
                    pageSize:10,
                    roleCode:""
                },
                roleCode:"",
                fields:[
                    {
                        label:"角色CODE",
                        prop:"roleCode",
                        width:100
                    },{
                        label:"角色名称",
                        prop:"roleName",
                        width:100
                    },{
                        label:"角色别名",
                        prop:"roleNickName",
                        width:110
                    },{
                        label:"备注",
                        prop:"remark",
                        width:120
                    }
                ],
                tableData: []
            }
        },
        created(){
            this.queryRoleList();
        },
        methods:{
            showDialog(){
                this.dialogVisible = true;
                this.queryParams.roleCode = '';
                this.roleCode = "";
                this.queryRoleList();
            },
            closeDialog(){
                this.dialogVisible = false
                this.loading = false
            },
            async queryRoleList(){
                this.loading = true;
                let res = await getRoleList(this.queryParams);
                let content = res.data;
                this.total = content.total;
                this.tableData = content.list;
                this.loading = false;
            },
            handleSizeChange(val) {
                this.queryParams.limit = val;
            },
            handleCurrentChange(val) {
                this.queryParams.pageNum = val;
            },
            handleDataChange(row){
                if(row == null){
                    return;
                }
                this.roleCode = row.roleCode;
            },
            submit(){
                if(this.roleCode == null || this.roleCode == ""){
                    this.$message.warning('请选择角色');
                    return;
                }
                this.closeDialog();
                this.$emit('success',this.roleCode)
            }
        }
    }
</script>
<style lang="scss" scoped>
    .drawer-panel{
        padding-left: 20px;
        padding-right: 20px;
    }
    .btn-group{
        display: flex;
        justify-content: right;
        .el-button{
            width: 50px;
        }
    }
    .el-form-item{
        height: 40px;
    }
     .search-item{
        float: left;
        margin: 0 20px 20px 0;
        span{
            color: #606266;
            font-size: 14px;
            margin-right: 0px;
        }
    }
</style>